package com.waq.service.impl;

import com.waq.enums.Sex;
import com.waq.mapper.UserMapper;
import com.waq.pojo.User;
//import com.waq.pojo.bo.UserBO;
import com.waq.service.UserService;
import com.waq.utils.DateUtil;
import com.waq.utils.MD5Utils;
import com.waq.utils.PositionUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    public UserMapper userMapper;
    @Autowired
    private Sid sid;

    public static final String USER_FACE = "https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=false&word=%E5%A4%B4%E5%83%8F%E7%8B%97&step_word=&hs=0&pn=54&spn=0&di=106370&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=2&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=-1&cs=2100562547%2C2360463242&os=4115449540%2C3804983400&simid=3439332543%2C159372542&adpicid=0&lpn=0&ln=3118&fr=&fmq=1630251392188_R&fm=result&ic=0&s=undefined&hd=0&latest=0&copyright=0&se=&sme=&tab=0&width=&height=&face=undefined&ist=&jit=&cg=head&bdtype=0&oriquery=&objurl=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fq_70%2Cc_zoom%2Cw_640%2Fimages%2F20180721%2Fa14d6a1cf05644cba8fa1a76479b7d11.jpeg%26refer%3Dhttp%3A%2F%2F5b0988e595225.cdn.sohucs.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Djpeg%3Fsec%3D1632843486%26t%3D85ce36896bcdb517086c0af7ab4af7ec&fromurl=ippr_z2C%24qAzdH3FAzdH3F4_z%26e3Bf5i7_z%26e3Bv54AzdH3FwAzdH3Fd9d9lllb0_c08ndb&gsm=38&rpstart=0&rpnum=0&islist=&querylist=&nojc=undefined";
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean queryUsernameIsExist(String username) {
        Example userExample = new Example(User.class);
        Example.Criteria userCriteria = userExample.createCriteria();
        //criteria条件，查询条件
        //property是User中username字段，value是传入的值
        userCriteria.andEqualTo("username", username);
        //通过查询条件Example去查询一条数据
        User result = userMapper.selectOneByExample(userExample);
        //查到结果为null返回false，不为null返回true，用户存在
        return result == null? false: true;
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public User queryUserForLogin(String username, String password) throws Exception {
        Example userExample = new Example(User.class);
        Example.Criteria userCriteria = userExample.createCriteria();
        //criteria条件，查询条件
        userCriteria.andEqualTo("username", username);
        userCriteria.andEqualTo("password", MD5Utils.getMD5Str(password));
        //通过查询条件Example去查询一条数据
        User result = userMapper.selectOneByExample(userExample);
        return result;
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public User register(String username, String password, String position) throws Exception {
        String userId = sid.nextShort();//设置主键id（固定16位字母数字混编的字符串）
        User user = new User();
        user.setUsername(username);
        try {
            user.setPassword(MD5Utils.getMD5Str(password));//将密码加密后，保存在数据库中
        } catch (Exception e) {
            e.printStackTrace();
        }
        user.setPositionId(PositionUtils.getPositionId(position));
        userMapper.insert(user);
        return user;
    }



}
